package interfazeak;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import Imagenes.IrudiakHasiera;

import db.AlarmaKud;
import db.BotikaKud;
import db.UpelaKud;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Hasiera extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Hasiera frame = new Hasiera();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Hasiera() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("HASIERA");
		setBounds(100, 100, 748, 451);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		IrudiakHasiera p = new IrudiakHasiera();
		p.setBorder(new EmptyBorder(5, 5, 5, 5));
		p.setLayout(new BorderLayout(0, 0));
		setContentPane(p);
		p.setLayout(null);

		JButton mahastiak = new JButton("MAHASTIAK");
		mahastiak.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Hasiera.this.mahastiak();
			}
		});
		mahastiak.setBounds(152, 50, 117, 25);
		p.add(mahastiak);

		JButton upategia = new JButton("UPATEGIA");
		
		upategia.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Hasiera.this.upategia();
			}
		});
		upategia.setBounds(152, 130, 117, 25);
		p.add(upategia);

		JButton biltegia = new JButton("BILTEGIA");
		biltegia.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Hasiera.this.biltegia();
			}
		});
		biltegia.setBounds(152, 210, 117, 25);
		p.add(biltegia);

		JButton konfig = new JButton("KONFIGURAZIOA");
		konfig.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				Hasiera.this.konfigurazioa();
			}
		});
		konfig.setBounds(450, 350, 160, 25);
		p.add(konfig); // bestela contentPane.add(konfig);

		alarmakbegiratu();
	}

	protected void konfigurazioa() {
		
		Konfigurazioa b = new Konfigurazioa();
		b.setVisible(true);
		this.dispose();
	}

	protected void biltegia() {
		
		Biltegia b = new Biltegia();
		b.setVisible(true);
		this.dispose();

	}

	protected void upategia() {
		
		Upategia b = new Upategia();
		b.setVisible(true);
		this.dispose();
	}

	private void alarmakbegiratu() {
		
		Date gaurkoeguna = new Date();
		Vector<String> alarmabektore = new Vector<String>();

		int i = 0;
		ResultSet rs = AlarmaKud.lortualarmak();

		try {
			while (rs.next()) {
				String alarma = rs.getString("alarma");
				int id = rs.getInt("idAlarma");
				alarmabektore.add(i, alarma);
				i = i + 1;
				// JOptionPane.showMessageDialog(Hasiera.this, alarma);
				int konfirmatu = JOptionPane.showConfirmDialog(Hasiera.this,
						alarma);

				if (JOptionPane.OK_OPTION == konfirmatu) {

					AlarmaKud.aldatuAlarma(id);
				}
				// hartu id alarma
				// 1era ipini konfirmatuta id alarma horregaz!!!
			}
		} catch (HeadlessException e) {
			
			e.printStackTrace();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}

	}

	protected void mahastiak() {
		
		Mahastiak_Lekuak b = new Mahastiak_Lekuak();
		b.setVisible(true);
		this.dispose();

	}
}
